package qf.sail.controller;

import qf.sail.contents.CodeMsg;
import qf.sail.entity.Account;
import qf.sail.service.OrderService;
import qf.sail.service.impl.OrderServiceImpl;
import qf.sail.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/generateOrder")
public class GenerateOrderServlet extends BaseServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = new OrderServiceImpl();
        Account account = (Account) req.getSession().getAttribute("account");
        //生成订单
        long orderId = orderService.generateOrder(account.getId());
        //生成订单子项
        long goodId = Long.parseLong(req.getParameter("goodId"));
        int count = Integer.parseInt(req.getParameter("count"));
        String color = req.getParameter("color");
        String size = req.getParameter("size");

        orderService.addOrderItem(goodId,orderId,size,color,count);

        //计算订单金额并更新
        orderService.updateOrderMoney(orderId);

        ResultVO resultVO = new ResultVO();
        resultVO.addData("orderId",orderId);
        repSuccess(resp,resultVO);

    }
}
